<?php

class Functions {

    // Database configuration
    private $dbConfig = Array();
    private $fieldsMap = Array();
    private $connection;
    private $downloadFilesFolder;
    private $rootItem;

    const ERROR_DATABASE_CONFIGURATION_NOT_PROVIDED = "Database configuration is not provided for XMLImporter!";
    const ERROR_XML_DATABASE_FIELDS_MAP_NOT_PROVIDED = "XML to database fields map is not provided for XMLImporter!";
    const ERROR_XML_FILE_PATH_NOT_PROVIDED = "XML file path is not provided for XMLImporter!";
    const ERROR_XML_ROOT_ITEM_NOT_PROVIDED = "XML file root item name is not provided!";

    public function __construct($dbConfig, $fieldsMap, $downloadFilesFolder, $rootItem)
    {
        if (isset($dbConfig)) {
            $this->dbConfig = $dbConfig;
            $this->connectToDatabase();
        } else {
            die(self::ERROR_DATABASE_CONFIGURATION_NOT_PROVIDED);
        }

        if (isset($fieldsMap)) {
            $this->fieldsMap = $fieldsMap;
        } else {
            die(self::ERROR_XML_DATABASE_FIELDS_MAP_NOT_PROVIDED);
        }

        if (isset($downloadFilesFolder)) {
            $this->downloadFilesFolder = $downloadFilesFolder;
        }

        if (isset($rootItem)) {
            $this->rootItem= $rootItem;
        } else {
            die(self::ERROR_XML_ROOT_ITEM_NOT_PROVIDED);
        }
    }

    private function connectToDatabase()
    {
        // Create Database connection
        $this->connection = mysqli_connect($this->dbConfig["host"], $this->dbConfig["user"], $this->dbConfig["password"], $this->dbConfig["database"]);

        // Check connection
        if (mysqli_connect_errno()) {
            die("Failed to connect to MySQL: " . mysqli_connect_error());
        }
    }

    // Move callbacks to separate class!
    public function getCategoryByTitle($title, $itemWrapper)
    {
        $result = $this->executeQuery("SELECT cat_id FROM tb_category WHERE cat_name = '$title'");
        if ($result->num_rows > 0) {
            $categoryRow = $result->fetch_assoc();
            return $categoryRow['cat_id'];
        }
        $this->executeQuery("INSERT INTO tb_category (cat_name, parent_id, active, cat_keyword) VALUES ('$title', 0, 1, '')");
        return mysqli_insert_id($this->connection);
    }

    public function getSubcategoryByTitle($title, $itemWrapper)
    {
        $primaryCategory = $this->getValue($itemWrapper, Array(Type::TYPE => ValueType::TAG,
            "value" => "category->primary",
            "action" => 'getCategoryByTitle'));

        $result = $this->executeQuery("SELECT cat_id FROM tb_category WHERE cat_name = '$title' AND parent_id = '$primaryCategory'");
        if ($result->num_rows > 0) {
            $categoryRow = $result->fetch_assoc();
            return $categoryRow['cat_id'];
        }
        $this->executeQuery("INSERT INTO tb_category (cat_name, parent_id, active, cat_keyword) VALUES ('$title', '$primaryCategory', 1, '')");
        return mysql_insert_id();
    }

    public function getAffilateLink($result, $itemWrapper)
    {
        $disallowed = array('http://', 'https://');
        foreach($disallowed as $item) {
            if(strpos($result, $item) === 0) {
                return str_replace($item, '', $result);
            }
        }
        return $result;
    }
}
?>